;(function () { 
	var vuePullLoading = {} 
	vuePullLoading.install = function (Vue,options) { 
		Vue.directive('pull', { 
			update: function (value) {
				acceptStatement:true
				this.callBack = value;
				this.isBottom = false;			//是否可以上拉
				this.isLoad = false;			//是否可以发送请求
				this.loadOk = false;			//是否在发送请求加载数据
				this.startY = 0;				//默认Y值
				this.result = 0;				//默认要移动的值
				this.deltaY = 0;
				this.lastPointY = 0;
				this.translate3d = "";
				this.curDom = this.el;			//当前元素
				var self = this;
				this.curDom.addEventListener('touchstart',function(e){
					self.touchStart(e);
				},false);
				this.curDom.addEventListener('touchmove',function(e){
					self.touchMove(e);
				},false);
				
				this.curDom.addEventListener('touchend',function(e){
					self.touchEnd(e);
				},false);	
				
				this.curDom.addEventListener('touchcancel',function(e){
					self.touchEnd(e);
				},false);	
			}, 
			touchMove:function(e){
				if(this.isBottom){			//当页面可以上拉 并且不是正在加载数据才允许上拉
					var box = e.currentTarget;				//获取到当前滑动元素
					
					this.deltaY = Math.abs(e.changedTouches[0].pageY - this.lastPointY);
					this.lastPointY = e.changedTouches[0].pageY;
					
					this.result = this.result + e.changedTouches[0].pageY - this.startY;		//计算出要移动的距离
					this.startY = e.changedTouches[0].pageY;
					this.result = this.result + this.deltaY / 1.5;
					this.result >= 0 ? this.result =0 : e.preventDefault();	//如果box要移动则禁止浏览器默认行为
					box.style.transition = '0s';			//取消css3动画
					this.translate3d = 'translate3d(0,'+this.result+'px,0)';
					this.curDom.style.transform = this.translate3d;
					this.curDom.style.webkitTransform = this.translate3d;
					console.log(this.deltaY)
				}
			},
			touchStart:function(e){
				this.bottom(e);		
				if(this.isBottom){			
					this.startY = e.changedTouches[0].pageY;	//如果当前是符合可以上拉的条件则重置默认的startY值
				}
			},
			touchEnd:function(e){
				if(this.result <= -150){
					this.loadOk = true;		//标注当前已经满足发送请求的条件
				}

				this.bottom(e);			//调用bottom方法，判断当前是不是符合上拉的条件
				if(this.isBottom){		//如果是  则。。。
					this.result = 0;	//还原result值
					var box = e.currentTarget;
					box.style.transition = '0.3s';	//为box加上css3动画效果
					if(this.loadOk){										//如果当前是满足发送请求的条件。。。	
						this.loadOk = false;								//先变回默认值
						this.isLoad = true;									//标注当前是在发送请求加载数据
						var self = this;
						this.callBack({
							success:function(){
								self.isLoad = false;
							}
						});					
					}
					this.translate3d = 'translate3d(0,0,0)';
					this.curDom.style.transform = this.translate3d;
					this.curDom.style.webkitTransform = this.translate3d;
				}
			},
			bottom:function(e){
				var box = e.currentTarget;
				var top = box.scrollTop;
				var windowHeight = box.offsetHeight;
				var documentHeight = box.firstElementChild.offsetHeight;
				var result = documentHeight-windowHeight-2;	
				top >= result? this.isBottom = true : this.isBottom = false;  //当用户滑倒页面最底部并且页面高度大于窗口高度则可以上拉
			}
		});
	} 
	if (typeof exports == "object") {
		module.exports = vuePullLoading;
	} else if (typeof define == "function" && define.amd) {
		define([], function(){ 
			return vuePullLoading ;
		})
	} else if (window.Vue) { 
		window.vuePullLoading = vuePullLoading;
		Vue.use(vuePullLoading);
	}
})();
